package handlers

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"like-chat/models"
	"strconv"
)

func Like(c *gin.Context) {
	user_id, _ := strconv.Atoi(c.PostForm("user_id"))
	new_id, _ := strconv.Atoi(c.PostForm("new_id"))
	data := models.Like{
		Model:  gorm.Model{},
		UserID: user_id,
		NewID:  new_id,
	}
	models.DB.Where("user_id = ?", user_id).Find(&data)
	if data.ID > 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "只能点赞一次",
		})
		return
	}
	err := models.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "点赞失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "点赞成功",
		})
		return
	}
}
